/*
 * input.c
 *
 *  Created on: 2012/09/20
 *      Author: shenming
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "core_input.h"
#include "Id.h"

INPUTINFO stInputInfo = { NULL, NULL, NULL, NULL, NULL, NULL };

static BOOL checkSpaceLine(PSZ line) {
	int i = 0;
	CHAR buf[OS_FULLPATH];
	strcpy(buf, line);
	while (buf[i] != '\0') {
		if (buf[i] != ' ' && buf[i] != '\t') {
			return FALSE;
		}
		i++;
	}
	return TRUE;
}

static VOID addToStrList_Ws(PSTRLIST *head, PSZ workspace, PSZ line) {
	PSTRLIST * node = NULL;
	PSTRLIST newNode = NULL;

	if(line[0]=='/' || line[0]=='\\')
	{
		line++;
	}

	if (*head == NULL ) {
		*head = (PSTRLIST) malloc(sizeof(STRLIST));
		(*head)->str = (PSZ) malloc(strlen(workspace) + strlen(line) + 1);
		strcpy((*head)->str, workspace);
		strcat((*head)->str, line);
		(*head)->next = NULL;
	} else {
		newNode = (PSTRLIST) malloc(sizeof(STRLIST));
		newNode->str = (PSZ) malloc(strlen(workspace) + strlen(line) + 1);
		strcpy(newNode->str, workspace);
		strcat(newNode->str, line);
		newNode->next = NULL;
		node = head;
		while ((*node)->next != NULL ) {
			node = &((*node)->next);
		}
		(*node)->next = newNode;
	}
}

static VOID addToStrList(PSTRLIST *head, PSZ line) {
	PSTRLIST * node = NULL;
	PSTRLIST newNode = NULL;

	if (*head == NULL ) {
		*head = (PSTRLIST) malloc(sizeof(STRLIST));
		(*head)->str = (PSZ) malloc(strlen(line) + 1);
		strcpy((*head)->str, line);
		(*head)->next = NULL;
	} else {
		newNode = (PSTRLIST) malloc(sizeof(STRLIST));
		newNode->str = (PSZ) malloc(strlen(line) + 1);
		strcpy(newNode->str, line);
		newNode->next = NULL;
		node = head;
		while ((*node)->next != NULL ) {
			node = &((*node)->next);
		}
		(*node)->next = newNode;
	}
}

INT analyzeInput(PSZ path, PSZ workspace) {
	FILE * file;
	CHAR line[OS_FULLPATH];

	BOOL bSrcIn = FALSE;
	BOOL bOut = FALSE;
	BOOL bDef = FALSE;
	BOOL bUDef = FALSE;
	BOOL bInc = FALSE;
	BOOL bSet = FALSE;

	file = fopen(path, "r");
	if (file == NULL ) {
		printf("can not open file %s\n", path);
		return EXIT_ERROR;
	}

	while (fgets(line, OS_FULLPATH - 1, file) != NULL ) {
		line[strlen(line) - 1] = '\0';
		if(line[strlen(line) - 1] == '\r'){
			line[strlen(line) - 1] = '\0';
		}
		if (line[0] != '#') {
			if (bSrcIn) {
				if (checkSpaceLine(line)) {
					bSrcIn = FALSE;
				} else {
					addToStrList_Ws(&(stInputInfo.inSrcListHead), workspace,
							line);
				}
			} else if (bOut) {
				stInputInfo.inDbPath = (PSZ) malloc(
						strlen(workspace) + strlen(line) + 1);
				strcpy(stInputInfo.inDbPath, workspace);
				strcat(stInputInfo.inDbPath, line);
				bOut = FALSE;
			} else if (bDef) {
				if (checkSpaceLine(line)) {
					bDef = FALSE;
				} else {
					addToStrList(&(stInputInfo.inDefListHead), line);
				}
			} else if (bUDef) {
				if (checkSpaceLine(line)) {
					bUDef = FALSE;
				} else {
					addToStrList(&(stInputInfo.inUDefListHead), line);
				}
			} else if (bInc) {
				if (checkSpaceLine(line)) {
					bInc = FALSE;
				} else {
					addToStrList_Ws(&(stInputInfo.inIncListHead), workspace,
							line);
				}
			} else if (bSet) {
				if (checkSpaceLine(line)) {
					bSet = FALSE;
				} else {
					addToStrList(&(stInputInfo.inSetListHead), line);
				}
			} else if (!strcmp(line, "[SRC-List]")) {
				bSrcIn = TRUE;
			} else if (!strcmp(line, "[OUT-SQLite]")) {
				bOut = TRUE;
			} else if (!strcmp(line, "[DOPTION]")) {
				bDef = TRUE;
			} else if (!strcmp(line, "[UDOPTION]")) {
				bUDef = TRUE;
			} else if (!strcmp(line, "[INCLUDE]")) {
				bInc = TRUE;
			} else if (!strcmp(line, "[SETTING]")) {
				bSet = TRUE;
			}
		}
	}

	fclose(file);
	return EXIT_SUCCESS;
}

static VOID freeStrList(PSTRLIST head) {
	PSTRLIST node = head;
	PSTRLIST tmp = NULL;
	if (node != NULL ) {
		while (node->next != NULL ) {
			tmp = node->next;
			if (node != NULL ) {
				free(node->str);
			}
			free(node);
			node = tmp;
		}
	}
}

VOID freeInputInfo( VOID) {
	freeStrList(stInputInfo.inSrcListHead);
	if (stInputInfo.inDbPath != NULL ) {
		free(stInputInfo.inDbPath);
	}
	freeStrList(stInputInfo.inDefListHead);
	freeStrList(stInputInfo.inUDefListHead);
	freeStrList(stInputInfo.inIncListHead);
	freeStrList(stInputInfo.inSetListHead);
}

